package com.kimehwa.chapter1.novice;

/**
 * @author xiaodongguan
 * @date 2022/02/22 15:12
 * @description addWater()
 * a.connectTo(b)
 **/
public class Container {
    private Container[] containers;

    private int containerNum;

    private double waterAmounts;


    public Container() {
        containers = new Container[1000];
        containerNum = 1;
        waterAmounts = 0;
    }


    public void addWater(Double water) {
        double average = water / containerNum;
        for (Container container : containers) {
            setAmount(container.getAmount() + average);
        }
    }

    // 这个联通分量也可能有很多个联通分量 曹, 联通分量的水量是一致的
    public void connectTo(Container container) {
        // 先判断一下是否是联通的
        for(int i = 0;i<containerNum;i++){
            for(int j = 0;j<container.getContainerNum();j++){
                if(containers[i] == container.getContainers()[j]){
                    return ;
                }
            }
        }
        double aveEve = (waterAmounts * containerNum + container.getContainerNum()
                * container.getAmount()) / (containerNum + container.getContainerNum());
        // 设置每一个联通分量的水，里面的每一个都要添加进去
        for (int i = 0; i < container.containerNum; i++) {
            for (int j = 0; j < container.getContainerNum(); j++) {
                // 遍历第一组的容器 都放进去
                containers[i].getContainers()[containerNum + j] = container.getContainers()[j];
                // 遍历第二组的容器 都放进去
                container.getContainers()[j].getContainers()[container.getContainerNum() + i] = containers[i];
            }
        }
        // 更新数量
        containerNum += container.getContainerNum();
        // 更新大小跟水量
        for (int i = 0; i < containerNum; i++) {
            containers[i].setContainerNum(containerNum);
            containers[i].setAmount(aveEve);
        }


    }


    public double getAmount() {
        return waterAmounts;
    }

    public void setAmount(double amount) {
        this.waterAmounts += amount;
    }

    public int getContainerNum() {
        return containerNum;
    }

    public void setContainerNum(int containerNum) {
        this.containerNum = containerNum;
    }

    public Container[] getContainers() {
        return containers;
    }

    public void setContainers(Container[] containers) {
        this.containers = containers;
    }


}
